﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class AnimationSelectionInfo : INotifyPropertyChanged
    {
        private int _leftIndexBounds, _rightIndexBounds;
        private AnimationLayer _currentAnimationLayer;

        public AnimationLayer CurrentAnimationLayer { get { return _currentAnimationLayer; } set { SetCurrentAnimationLayer(value); } }
        public int LeftIndexBounds { get { return _leftIndexBounds; } set { _leftIndexBounds = value; OnPropertyChanged("LeftIndexBounds"); } }
        public int RightIndexBounds { get { return _rightIndexBounds; } set { _rightIndexBounds = value; OnPropertyChanged("RightIndexBounds"); } }
        public FrameData CurrentFrameData { get; private set; }
        public AnimationData CurrentAnimationData { get { return (CurrentAnimationLayer != null) ? CurrentAnimationLayer.CurrentAnimationData : null; } }
        public int CurrentIndex { get; private set; }
        public int CurrentFrameIndex { get { return (CurrentAnimationLayer != null) ? CurrentIndex - CurrentAnimationLayer.Offset : -1; } }
        public ObservableCollection<AnimationLayer> BackgroundAnimationCollection { get; private set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public AnimationSelectionInfo()
        {
            BackgroundAnimationCollection = new ObservableCollection<AnimationLayer>();
            LeftIndexBounds = 1;
            RightIndexBounds = 1;
        }

        public void UpdateSelectionInfo(AnimationLayer currentAnimationLayer, IEnumerable<AnimationLayer> backgroundAnimationLayers)
        {
            UpdateAnimationProperties(currentAnimationLayer);

            ReloadAnimationCollection(backgroundAnimationLayers);
        }

        private void UpdateAnimationProperties(AnimationLayer animationLayer)
        {
            if (animationLayer == null)
            {
                CurrentAnimationLayer = null;
                CurrentFrameData = null;
                CurrentIndex = -1;
            }
            else
            {
                CurrentAnimationLayer = animationLayer;
                CurrentFrameData = animationLayer.CurrentFrameData;

                if (CurrentAnimationLayer.CurrentFrameData == null)
                    CurrentIndex = -1;
                else
                    CurrentIndex = CurrentAnimationLayer.CurrentAnimationData.FrameDataCollection.IndexOf(CurrentFrameData) + CurrentAnimationLayer.Offset;
            }

            OnPropertyChanged("CurrentAnimationLayer");
            OnPropertyChanged("CurrentFrameData");
            OnPropertyChanged("CurrentAnimationData");
            OnPropertyChanged("CurrentIndex");
        }

        private void ReloadAnimationCollection(IEnumerable<AnimationLayer> backgroundAnimationLayers)
        {
            BackgroundAnimationCollection.Clear();

            foreach (AnimationLayer animationLayer in backgroundAnimationLayers)
                BackgroundAnimationCollection.Add(animationLayer);
        }

        private void SetCurrentAnimationLayer(AnimationLayer value)
        {
            if (_currentAnimationLayer != null)
                _currentAnimationLayer.PropertyChanged -= new PropertyChangedEventHandler(CurrentAnimationLayer_PropertyChanged);

            _currentAnimationLayer = value;

            if (_currentAnimationLayer != null)
                _currentAnimationLayer.PropertyChanged += new PropertyChangedEventHandler(CurrentAnimationLayer_PropertyChanged);
        }

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void CurrentAnimationLayer_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Distance")
            {
                CurrentIndex = CurrentAnimationLayer.CurrentAnimationData.FrameDataCollection.IndexOf(CurrentFrameData) + CurrentAnimationLayer.Offset;
                OnPropertyChanged("CurrentIndex");
            }
        }
    }
}
